using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using UwinEducation.API.Attributes;
using UwinEducation.Application.Common.Interfaces.Orders;
using UwinEducation.Application.Dtos.Orders;
using UwinEducation.Shared.Models;

namespace UwinEducation.API.Controllers
{
    /// <summary>
    /// 订单支付管理控制器
    /// </summary>
    /// <remarks>
    /// 该控制器提供了订单支付信息的增删改查功能，
    /// 包括查询支付详情、按订单查询支付列表、分页查询支付记录、创建、更新、删除支付记录等操作。
    /// </remarks>
    [Authorize]
    //[RequirePermission("student.order")] // 假设支付管理权限与订单一致
    [ApiController]
    [Route("api/[controller]/[action]")]
    public class OrderPaymentController : ControllerBase
    {
        private readonly IOrderPaymentService _orderPaymentService;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="orderPaymentService">订单支付服务接口</param>
        public OrderPaymentController(IOrderPaymentService orderPaymentService)
        {
            _orderPaymentService = orderPaymentService;
        }

        /// <summary>
        /// 根据ID获取订单支付详情
        /// </summary>
        /// <remarks>
        /// 根据支付记录的唯一ID查询详细信息。
        /// </remarks>
        /// <param name="request">包含支付ID的请求参数</param>
        /// <returns>订单支付详情</returns>
        [HttpPost]
        public async Task<ApiResponse<OrderPaymentDto>> GetById([FromBody] IdRequest request)
        {
            var payment = await _orderPaymentService.GetByIdAsync(request.Id);
            return ApiResponse<OrderPaymentDto>.Ok(payment);
        }

        /// <summary>
        /// 根据订单ID获取支付记录列表
        /// </summary>
        /// <remarks>
        /// 获取指定订单的所有支付记录，用于查看订单的支付历史。
        /// </remarks>
        /// <param name="request">包含订单ID的请求参数</param>
        /// <returns>订单支付列表</returns>
        [HttpPost]
        public async Task<ApiResponse<List<OrderPaymentDto>>> GetByOrderId([FromBody] IdRequest request)
        {
            var payments = await _orderPaymentService.GetByOrderIdAsync(request.Id);
            return ApiResponse<List<OrderPaymentDto>>.Ok(payments);
        }

        /// <summary>
        /// 获取订单支付分页列表
        /// </summary>
        /// <remarks>
        /// 根据筛选条件（如订单ID、支付方式、日期范围等）获取分页的订单支付记录。
        /// </remarks>
        /// <param name="request">分页及筛选请求参数</param>
        /// <returns>订单支付分页列表</returns>
        [HttpPost]
        public async Task<ApiResponse<PagedResult<OrderPaymentDto>>> GetPagedList([FromBody] OrderPaymentPageRequestDto request)
        {
            var result = await _orderPaymentService.GetPagedListAsync(request);
            return ApiResponse<PagedResult<OrderPaymentDto>>.Ok(result);
        }

        /// <summary>
        /// 创建订单支付记录
        /// </summary>
        /// <remarks>
        /// 为指定订单创建一条新的支付记录。创建成功后，会自动更新订单的支付总额和支付状态。
        /// </remarks>
        /// <param name="createDto">创建订单支付请求参数</param>
        /// <returns>创建成功的订单支付记录详情</returns>
        [HttpPost]
        [RequirePermission("student.order")] // 假设支付管理权限与订单一致
        public async Task<ApiResponse<OrderPaymentDto>> Create([FromBody] CreateOrderPaymentDto createDto)
        {
            var payment = await _orderPaymentService.CreateAsync(createDto);
            return ApiResponse<OrderPaymentDto>.Ok(payment);
        }

        /// <summary>
        /// 更新订单支付记录
        /// </summary>
        /// <remarks>
        /// 更新现有订单支付记录的信息，如支付金额、支付方式、凭证号、备注等。通常不允许修改关联的订单ID。
        /// 更新后，会自动更新订单的支付总额和支付状态。
        /// </remarks>
        /// <param name="updateDto">更新订单支付请求参数</param>
        /// <returns>更新后的订单支付记录详情</returns>
        [HttpPost]
        [RequirePermission("student.order")] // 假设支付管理权限与订单一致
        public async Task<ApiResponse<OrderPaymentDto>> Update([FromBody] UpdateOrderPaymentDto updateDto)
        {
            var payment = await _orderPaymentService.UpdateAsync(updateDto);
            return ApiResponse<OrderPaymentDto>.Ok(payment);
        }

        /// <summary>
        /// 删除订单支付记录
        /// </summary>
        /// <remarks>
        /// 删除指定ID的订单支付记录。删除后，会自动更新订单的支付总额和支付状态。
        /// </remarks>
        /// <param name="request">包含支付ID的请求参数</param>
        /// <returns>操作结果</returns>
        [HttpPost]
        [RequirePermission("student.order")] // 假设支付管理权限与订单一致
        public async Task<ApiResponse<object>> Delete([FromBody] IdRequest request)
        {
            await _orderPaymentService.DeleteAsync(request.Id);
            return ApiResponse<object>.Ok(null, "删除成功");
        }

        /// <summary>
        /// 计算订单总支付金额
        /// </summary>
        /// <remarks>
        /// 计算指定订单的所有有效支付记录的总金额。
        /// </remarks>
        /// <param name="request">包含订单ID的请求参数</param>
        /// <returns>订单的总支付金额</returns>
        [HttpPost]
        public async Task<ApiResponse<decimal>> GetTotalPaidAmount([FromBody] IdRequest request)
        {
            var totalPaid = await _orderPaymentService.GetTotalPaidAmountAsync(request.Id);
            return ApiResponse<decimal>.Ok(totalPaid);
        }
    }
} 